﻿/* 2012/10/12 */
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace Cosmos.Windows.Forms
{
    public class TabControlInsertionMark
    {
        TabControl _tabControl;

        public TabControlInsertionMark(TabControl tabControl)
        {
            _tabControl = tabControl;
        }

        public void InsertAtClientPosition(Point position)
        {
            Index = -1;
            InsertAfterTab = false;
            for (int i = 0; i < _tabControl.TabCount; i++)
            {
                Rectangle tabBound = _tabControl.GetTabRect(i);
                if (tabBound.Contains(position))
                {
                    Index = i;
                    if ((tabBound.Left + tabBound.Width / 2) >= position.X)
                        InsertAfterTab = false;
                    else
                        InsertAfterTab = true;
                    break;
                }
            }

            InTabControl = false;
            if (position.X >= 0 && _tabControl.Width >= position.X
                && position.Y >= 0 && _tabControl.Height >= position.Y)
            {
                InTabControl = true;
            }
        }

        public void InsertAtScreenPosition(Point position)
        {
            InsertAtClientPosition(_tabControl.PointToClient(position));
        }

        /// <summary>
        /// Gets or sets a value indicating whether the new tab will be inserted after
        /// the tab with the index specified by the Index property.
        /// </summary>
        public bool InsertAfterTab
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the index of the item next to which the new tab will be inserted.
        /// </summary>
        public int Index
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether the new tab has been moved in tab control.
        /// </summary>
        public bool InTabControl
        {
            get;
            set;
        }
    }
}
